﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using SNSpirit.Domain.User;

namespace SNSpirit.Domain.Rss
{
    [Serializable]
    [XmlRoot(ElementName = "rssFeed", Namespace = DomainUtillity.Namespace)]
    public class RssFeed : DomainEntity, IEquatable<RssFeed>
    {
        public RssFeed()
        {
            this.Channel = new RssChannel();
        }
        /// <summary>
        /// Rss feed version
        /// </summary>
        [XmlAttribute(AttributeName = "version", Namespace = DomainUtillity.Namespace)]
        public string Version { get; set; }
        /// <summary>
        /// UpdateUri
        /// </summary>
        [XmlAttribute(AttributeName = "updateUri", Namespace = DomainUtillity.Namespace)]
        public Uri UpdateUri { get; set; }
        /// <summary>
        /// Update Interval
        /// </summary>
        [XmlAttribute(AttributeName = "title", Namespace = DomainUtillity.Namespace)]
        public TimeSpan UpdateInterval { get; set; }
        /// <summary>
        /// If rss feed source need user info (such as auth info)
        /// </summary>
        [XmlElement(ElementName = "user", Namespace = DomainUtillity.Namespace)]
        public UserEntity User { get; set; }
        /// <summary>
        /// Rss collection
        /// </summary>
        [XmlElement(ElementName = "title", Namespace = DomainUtillity.Namespace)]
        public RssChannel Channel { get; private set; }

        public override string ToString()
        {
            return string.Format("Rssfeed@{0}{1}",
                this.UpdateUri != null ? this.UpdateUri.ToString() : string.Empty,
                this.User != null ? this.User.ToString() + ':' : string.Empty
                );
        }

        public override int GetHashCode()
        {
            if (this.UpdateUri == null)
                return 0;
            else
                return this.UpdateUri.GetHashCode();
        }

        #region Equals

        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is RssFeed))
                return false;
            else
                return this.Equals(obj as RssFeed);
        }

        public bool Equals(RssFeed other)
        {
            if (other == null)
                return false;
            else
                return this.UpdateUri == other.UpdateUri;
        }

        #endregion
    }
}
